package com.xc.ksxt.controller;

import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.ChoiceCopy;
import com.xc.db.tables.records.ChoiceCopyRecord;
import com.xc.ksxt.entity.ChoiceCopyEntity;
import com.xc.ksxt.entity.QuestionCopyEntity;
import com.xc.ksxt.service.ChoiceCopyService;
import com.xc.ksxt.service.QuestionCopyService;
import com.xc.ksxt.vo.ChoiceCopyVO;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.UpdateQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/choiceCopy")
public class ChoiceCopyController {

    @Autowired
    private DSLContext dsl;
    @Autowired
    private ChoiceCopyService choiceCopyService;
    private ChoiceCopy CHOICECOPYTB = ChoiceCopy.CHOICE_COPY.as("CHOICECOPYTB");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<ChoiceCopyVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<ChoiceCopyVO> ltr = new LayuiTableResponse<>();
        Condition cond = CHOICECOPYTB.ID.notEqual(-1L);
        String questionCopyId = request.getParameter("questionCopyId");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if(StringUtils.isEmpty(questionCopyId)) {
        	cond = cond.and(CHOICECOPYTB.QUESTION_COPY_ID.eq(Long.valueOf(questionCopyId)));
        }
        Long count = choiceCopyService.count(cond);
        ltr.setCount(count);
        PageInfo<ChoiceCopyEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> list = choiceCopyService.select(cond, pageInfo,CHOICECOPYTB.INDEX_LETTER.asc());
        List<ChoiceCopyVO> choiceCopyEntityList = list.into(ChoiceCopyVO.class);
        for (ChoiceCopyVO choiceCopyEntity : choiceCopyEntityList) {
        	
        }
        ltr.setData(choiceCopyEntityList);
        return ltr;
    }

    @RequestMapping({"/getById"})
    public ChoiceCopyEntity getById(Long id) {
        ChoiceCopyEntity entity = choiceCopyService.getEntityById(id);
        return entity;
    }

    @RequestMapping({"/save"})
    public ResponseEntity<ChoiceCopyEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
    	ResponseEntity<ChoiceCopyEntity> res = new ResponseEntity<>();
    	ChoiceCopyEntity choiceCopyEntity = Helper.map2Bean(new ChoiceCopyEntity(), params);
    	choiceCopyService.save(choiceCopyEntity);
        return res;
    }

    @CrossOrigin
    @RequestMapping({"/answer"})
    public ResponseEntity<ChoiceCopyEntity> answer(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
    	ResponseEntity<ChoiceCopyEntity> res = new ResponseEntity<>();
    	String id = params.get("id");
    	String selected = params.get("selected");
    	ChoiceCopyEntity choiceCopyEntity = choiceCopyService.getEntityById(Long.valueOf(id));
    	QuestionCopyEntity qce = QuestionCopyService.getFromCaches(choiceCopyEntity.getQuestionCopyId());
    	if(qce.getQuestionType().intValue()==1) {//如果是单选题，先把所有选项设置为未选择
    		UpdateQuery<ChoiceCopyRecord> q = dsl.updateQuery(CHOICECOPYTB);
    		q.addConditions(CHOICECOPYTB.QUESTION_COPY_ID.eq(Long.valueOf(choiceCopyEntity.getQuestionCopyId())));
    		q.addValue(CHOICECOPYTB.SELECTED, 2);
    		q.execute();
    	}
    	choiceCopyEntity.setSelected(Integer.valueOf(selected));
    	choiceCopyService.update(choiceCopyEntity);
    	return res;
    }
    @RequestMapping({"/delete"})
    public LayuiTableResponse<ChoiceCopyEntity> delete(@RequestParam("ids[]") Integer[] ids) {
        LayuiTableResponse<ChoiceCopyEntity> ltr = new LayuiTableResponse<>();
       
        return ltr;
    }

}
